﻿using Corina.Commands;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Linq;

namespace Corina.Lantern.Hub.Viewmodels
{
    public class BuyDeviceViewmodel : ViewmodelBase
    {
        public BuyDeviceViewmodel() { 
            DeviceClient.GetUnsaleDeviceCompleted+=GetUnsaleDeviceCompleted;
            DeviceClient.GetAllDeviceKindCompleted += GetAllDeviceKindCompleted;
            if(DataContext.IsValid)
            {
                LoadData();
                LoadRelatedData();
            }
        }

        private void GetAllDeviceKindCompleted(object sender, DataService.GetAllDeviceKindCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                DeviceKindCollection = new ObservableCollection<DeviceKindInfo>(e.Result.Entities.OfType<DeviceKindInfo>());
            }
        }

        private void GetUnsaleDeviceCompleted(object sender, DataService.GetUnsaleDeviceCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == ResultValue.Succeed)
                {
                    var items = (from t in e.Result.DataObjects
                                 select new
                                 {
                                     ItemIndex = t["ROW_NUMBER"],
                                     Id = t["Id"],
                                     DeviceSn = (string)t["DeviceSn"],
                                     Kind = (string)t["Kind"],
                                     Transfer = (string)t["DataModule"],
                                     ManufactureDate = (DateTime)t["ManufactureDate"],
                                     RatePrice = Convert.ToDouble(t["RatePrice"]),
                                 });
                    ItemsCollection = new ObservableCollection<object>(items);
                    ItemsCount = e.Result.TotalCount;
                }
            }
            IsBusy = false;
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            DeviceClient.GetUnsaleDeviceAsync(KindId, Index, 20);
        }

        protected void LoadRelatedData()
        {
            DeviceClient.GetAllDeviceKindAsync();
        }

        #region Search
        private int _kindId = 0;
        public int KindId
        {
            get
            {
                return _kindId;
            }
            set
            {
                _kindId = value;
                RaisePropertyChanged(() => KindId);
            }
        }

        private DeviceKindInfo _selectedDeviceKind;
        public DeviceKindInfo SelectedDeviceKind
        {
            get { return _selectedDeviceKind; }
            set
            {
                _selectedDeviceKind = value;
                RaisePropertyChanged(() => SelectedDeviceKind);
                KindId = SelectedDeviceKind == null ? 0 : SelectedDeviceKind.Id;
            }
        }
        #endregion

        #region Collection
        private ObservableCollection<object> _itemsCollection;
        public ObservableCollection<object> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }

        private ObservableCollection<DeviceKindInfo> _deviceKindCollection;
        public ObservableCollection<DeviceKindInfo> DeviceKindCollection
        {
            get { return _deviceKindCollection; }
            set
            {
                _deviceKindCollection = value;
                RaisePropertyChanged(() => DeviceKindCollection);
            }
        }
        #endregion

        #region ICommand
        public ICommand LoadCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        LoadData();
                    }
                };
            }
        }
        #endregion

        #region Buy
        public void AddToCar(object sender, RoutedEventArgs e)
        {
            object obj = (sender as FrameworkElement).DataContext;
            ShopingCollection.Add(obj);
            ItemsCollection.Remove(obj);
            RaisePropertyChanged(() => TotalAmount);
        }

        public ICommand RemoveCarItemCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    ShopingCollection.Remove(t);
                    ItemsCollection.Add(t);
                    RaisePropertyChanged(() => TotalAmount);
                });
            }
        }

        private ObservableCollection<object> _shopingCollection = new ObservableCollection<object>();
        public ObservableCollection<object> ShopingCollection
        {
            get { return _shopingCollection; }
            set
            {
                _shopingCollection = value;
                RaisePropertyChanged(() => ShopingCollection);
            }
        }

        public double TotalAmount
        {
            get
            {
                if(ShopingCollection.Count==0)
                {
                    return 0.00;
                }
                double amount = 0.00;
                ShopingCollection.ToList().ForEach(t => {
                    amount = amount + (double)GetObjectPropertyValue(t, "RatePrice");
                });
                return amount;
            }
        }
        #endregion
    }
}
